<?php
namespace app\mini\controller\v1\shop;

use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\logic\CarLogic;
use app\common\model\InterfacePermission;
use app\common\model\ShopMemberPermission;
use app\mini\controller\Common;
use app\common\model\Shop;
use think\Env;
use think\Request;

class Index extends Common
{

    public $CarLogic;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->CarLogic = new CarLogic();
    }

    /**
     * 商家端首页
     */
    public function index()
    {
        $res = $this->exception(function () {
            $shopInfo = Shop::field('logo,shop_name,business_start_time,business_end_time,money,fixed_amount')->find($this->member_info_arr['default_shop_id']); //获取店铺信息
            $permission = ShopMemberPermission::getMemberPermission($this->member_info_arr['member_id'],$this->member_info_arr['default_shop_id']); //获取权限菜单列表
            $shopInfo['balance_show'] = 0;
            $shopInfo['wait_audit_num'] = \app\common\model\Order::where(['shop_id'=>MemberVo::instance()->getShopId(),'order_status'=>2])->count();
            $shopInfo['recharge_success_num'] = \app\common\model\Recharge::where(['shop_id'=>MemberVo::instance()->getShopId(),'status'=>2])->count();//新充值完成数
            $shopInfo['business_time_desc'] = config('site.business_time_desc');
            if ($this->member_info_arr['role_id'] == 1)
                $shopInfo['balance_show'] =1;
            $service = [];
            if (!empty($permission)){
                $key_arr = array_column($permission->toArray(),'id');
                if (in_array(7,$key_arr)){
                    $service = ['page_url'=>InterfacePermission::where('id',7)->value('page_url'),'image'=>Env::get('app.app_host').'/uploads/shop_menu/service.png'];
                }
            }
            return ['info'=>$shopInfo,'menu'=>$permission,'service'=>$service];
        });
        return $this->response($res);
    }


    /**
     * 商家端首页搜索接口
     */
    public function search()
    {
        $res = $this->exception(function () {
            return $this->CarLogic->search(request()->param());
        });
        return $this->response($res);
    }

    public function ocr()
    {
        $res = $this->exception(function () {
            $file = $this->request->file('file');
            $res = file_get_contents($file->getInfo()['tmp_name']);
            $client = new \AipOcr(Env::get('baidu.appId'),Env::get('baidu.apiKey'),Env::get('baidu.secretKey'));
            $info = $client->licensePlate($res);
            if (isset($info['words_result']))
                return  $info['words_result']['number'];
            $info = $client->vinCode($res);
            if (isset($info['words_result']))
                return $info['words_result'][0]['words'];
            throw new ApiException('系统错误',404);
        });
        return $this->response($res);
    }
}
